package cn.easybuy.utils;

import java.io.Serializable;

public class CommonResult<T> implements Serializable {
    private long code;      //返回状态代码
    private String message;     //返回状态信息
    private T data;     //返回的数据

    public CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResult(long code, String message) {
        this.code = code;
        this.message = message;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 默认成功信息且无数据返回的方法
     * @return
     */
    public static <T> CommonResult success(){
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    /**
     * 默认成功信息且有数据返回的方法
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult success(T data){
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    /**
     * 自定义成功信息且无数据返回的方法
     * @param message
     * @return
     */
    public static CommonResult success(String message){
        return new CommonResult(ResultCode.SUCCESS.getCode(), message);
    }

    /**
     * 自定义成功信息且有数据返回的方法
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult success(String message,T data){
        return new CommonResult(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     * 默认失败信息的方法
     * @return
     */
    public static CommonResult failed(){
        return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    /**
     * 自定义失败信息的方法
     * @param message
     * @return
     */
    public static CommonResult failed(String message){
        return new CommonResult(ResultCode.FAILED.getCode(),message);
    }

    /**
     * 自定义失败代码和信息的方法
     * @param code
     * @param message
     * @return
     */
    public static CommonResult failed(long code,String message){
        return new CommonResult(code,message);
    }
}
